package org.edu.modules.stu.biye.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.dto.ExcelPo;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.stu.base.entity.VeBaseStudent;
import org.edu.modules.stu.base.service.VeBaseStudentService;
import org.edu.modules.stu.biye.dto.VeStuGraduationDto;
import org.edu.common.dto.AllDto;
import org.edu.modules.stu.biye.entity.VeStuGraduation;
import org.edu.modules.stu.biye.service.VeStuGraduationService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.stu.common.entity.VeBaseAppUser;
import org.edu.modules.stu.common.service.VeBaseAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = {"毕业查询管理"})
@RestController
@RequestMapping("/stu/veStuGraduation")
@Slf4j
public class VeStuGraduationController {

    @Autowired
    private VeStuGraduationService veStuGraduationService;

    @Autowired
    private VeBaseAppUserService veBaseAppUserService;

    @Autowired
    private VeBaseStudentService veBaseStudentService;

    @ApiOperation("分页查询毕业查询管理")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeStuGraduation> page) {
        QueryWrapper<VeStuGraduation> queryWrapper = page.buildQueryWrapper(VeStuGraduation.class);
        //查询未毕业的数据
        queryWrapper.isNull("jsxym");
        return Result.OK(veStuGraduationService.page(page, queryWrapper));
    }

    /**
     * 查询所有
     */
    @ApiOperation("查询所有")
    @PostMapping("/all")
    public Result<?> all(@RequestBody AllDto<VeStuGraduation> page) {
        QueryWrapper<VeStuGraduation> queryWrapper = page.buildQueryWrapper(VeStuGraduation.class);
        List<VeStuGraduation> list = veStuGraduationService.listByWrapper(queryWrapper);
        return Result.OK(list);
    }

    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeStuGraduation> page, HttpServletResponse resp) {
        QueryWrapper<VeStuGraduation> queryWrapper = page.buildQueryWrapper(VeStuGraduation.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("学生id", "stuId"));
        list.add(new ExcelPo("院系ID", "falId"));
        list.add(new ExcelPo("院系名称", "falName"));
        list.add(new ExcelPo("专业ID", "specId"));
        list.add(new ExcelPo("专业名称", "specName"));
        list.add(new ExcelPo("年级ID", "gradeId"));
        list.add(new ExcelPo("班级ID", "bjId"));
        list.add(new ExcelPo("班级名称", "bjName"));
        list.add(new ExcelPo("身份证号，唯一", "idcard"));
        list.add(new ExcelPo("学号", "code"));
        list.add(new ExcelPo("姓名", "name"));
        list.add(new ExcelPo("性别码1男生2女生", "sex"));
        list.add(new ExcelPo("出生日期", "csDate"));
        list.add(new ExcelPo("入学年月", "rxDate"));
        list.add(new ExcelPo("学制;与学制表关联", "xz"));
        list.add(new ExcelPo("学制名称", "xzName"));
        list.add(new ExcelPo("结束学业年月", "jsxyDate"));
        list.add(new ExcelPo("结束学业码（1=>毕业，2=>结业，3=>未结业，4=>肄业）GB/T 14946.1 －2009 附录 A.27，默认为空，待录入", "jsxym"));
        list.add(new ExcelPo("结束学业原因说明", "jsxyReson"));
        list.add(new ExcelPo("毕业证书号", "byZsh"));
        list.add(new ExcelPo("学位证书号", "xwZsh"));
        list.add(new ExcelPo("结业证书号", "jyZsh"));
        list.add(new ExcelPo("获毕业证书日期", "byDate"));
        list.add(new ExcelPo("获学位证书日期", "xwDate"));
        list.add(new ExcelPo("获结业证书日期", "jyDate"));
        list.add(new ExcelPo("证书打印次数，0为未打印", "bookPrintTimes"));
        list.add(new ExcelPo("预先生成的毕业证书编号", "preByzsh"));
        PoiUtils.downloadFile(resp, veStuGraduationService.page(page, queryWrapper).getRecords(), list);
        return Result.OK();
    }

    @AutoLog(value = "毕业查询管理-分页列表查询")
    @ApiOperation(value = "毕业查询管理-分页列表查询", notes = "毕业查询管理-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeStuGraduation entity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeStuGraduation> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeStuGraduation> page = new PageDto<VeStuGraduation>(pageNo, pageSize);
        queryWrapper.isNotNull("jsxym");
        IPage<VeStuGraduation> pageList = veStuGraduationService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeStuGraduation dto = veStuGraduationService.findDtoById(id);
        if (dto == null) {
            //判断是否是学生 如果是学生 查看自己的信息
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            //根据用户id查用户信息
            VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
            if (appUser.getUserType().contains("2")) {
                dto = veStuGraduationService.stuDto(appUser.getUserId());
            }
            if (dto == null) {
                return Result.error("未找到对应数据");
            }
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeStuGraduationDto dto) {
        veStuGraduationService.insertOrUpdate(dto, true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeStuGraduationDto dto) {
        veStuGraduationService.insertOrUpdate(dto, false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        veStuGraduationService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value = "更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        /*LambdaUpdateWrapper<VeStuGraduation> updateWrapper = new LambdaUpdateWrapper<>();
        if (statusDto.getIds() != null && statusDto.getIds().size() > 0) {
            updateWrapper.set(VeStuGraduation::getJsxym, statusDto.getStatus());
            updateWrapper.in(VeStuGraduation::getId, statusDto.getIds());
            veStuGraduationService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        } else {
            return Result.error("未选择!");
        }*/
        veStuGraduationService.batchGraduate(statusDto);
        return Result.OK("批量更新成功!");
    }


}
